package LC.nums.binarySearch;

public class LC35 {


    public static void main(String[] args) {
        int[] nums = new int[]{5, 7, 7, 8, 8, 10};
        int target = 6;

        int temp = two.binarySearch(nums, target);
        int[] ints = new int[]{-1, -1};

        if (temp == -1) {
            System.out.println(ints[0]);
            System.out.println(ints[1]);
        } else if (nums.length <= 2) {
            System.out.println(1);
        } else if (nums[temp - 1] == target) {
            ints[0] = temp - 1;
            ints[1] = temp;
        } else if (nums[temp + 1] == target) {
            ints[0] = temp;
            ints[1] = temp + 1;
        }

        System.out.println(ints[0]);
        System.out.println(ints[1]);

    }

    static class two {
        public int searchInsert(int[] nums, int target) {
//        int a = 0;
//        a = (0 + 3) / 2;
//        System.out.println(a); //a=1
            int left = 0;
            int right = nums.length - 1;
            while (left <= right) {
                int middle = left + ((right - left) / 2);
                if (nums[middle] < target) {
                    left = middle + 1;
                } else if (nums[middle] > target) {
                    right = middle - 1;
                } else {
                    return middle;
                }
            }
            return right + 1;
        }

        public int searchInsertTwo(int[] nums, int target) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] >= target) {
                    return i;
                }
            }
            return nums.length;
        }


        /**
         * @param nums
         * @param target
         * @return
         */
        public static int binarySearch(int[] nums, int target) {
            int left = 0;
            int right = nums.length - 1;
            while (left <= right) {
                int middle = left + ((right - left) / 2);
                if (nums[middle] < target) {
                    left = middle + 1;
                } else if (nums[middle] > target) {
                    right = middle - 1;
                } else {
                    return middle;
                }
            }
            return -1;
        }
    }
}



